package com.angis.fx.util;


public class OffsetInChina
    {
	
	
	 	
        //用于从GPS坐标转换为偏移后坐标
        public static GeoLatLng fromEarthToMars(GeoLatLng earth)
        {
            GeoPoint ptOffset = getOffset(earth.latitude, earth.longitude);
            if (ptOffset.x != 0 || ptOffset.y != 0)
            {
            	int[] lPixelArray = TileSystem.LatLongToPixelXY(earth.latitude, earth.longitude, 18);
                GeoPoint pt = new GeoPoint(lPixelArray[0], lPixelArray[1]);
                pt.x += ptOffset.x;
                pt.y += ptOffset.y;
                double[] lGeoArray = TileSystem.PixelXYToLatLong(pt.x, pt.y, 18);
                return new GeoLatLng(lGeoArray[1], lGeoArray[0]);
            }
            else
            {
                return new GeoLatLng(earth.latitude, earth.longitude);
            }
        }
        //用于将偏移后坐标转成真实的坐标
        public static GeoLatLng fromMarToEarth(GeoLatLng mars)
        {
            GeoPoint ptOffset = getOffset(mars.latitude, mars.longitude);
            if (ptOffset.x != 0 || ptOffset.y != 0)
            {
                int[] lPixelArray = TileSystem.LatLongToPixelXY(mars.latitude, mars.longitude, 18);
                GeoPoint pt = new GeoPoint(lPixelArray[0], lPixelArray[1]);
                pt.x -= ptOffset.x;
                pt.y -= ptOffset.y;
                double[] lGeoArray = TileSystem.PixelXYToLatLong(pt.x, pt.y, 18);
                return new GeoLatLng(lGeoArray[1], lGeoArray[0]);
            }
            else
            {
                return new GeoLatLng(mars.latitude, mars.longitude);
            }
        }


        //这个函数用于将需要查询的经纬度转成最近的0.01分度值，无插值
        //也可以自行实现插值
        private static GeoPoint getQueryLocation(double latitude, double longitude)
        {
            int lat = (int)(latitude * 100);
            int lng = (int)(longitude * 100);
            double lat1 = ((int)(latitude * 1000 + 0.499999)) / 10.0;
            double lng1 = ((int)(longitude * 1000 + 0.499999)) / 10.0;
            for (double x = longitude; x < longitude + 1; x += 0.5)
            {
                for (double y = latitude; x < latitude + 1; y += 0.5)
                {
                    if (x <= lng1 && lng1 < (x + 0.5) && lat1 >= y && lat1 < (y + 0.5))
                    {
                        return new GeoPoint((int)(x + 0.5), (int)(y + 0.5));
                    }
                }
            }
            return new GeoPoint(lng, lat);
        }


        private static GeoPoint getOffset(double longitude, double latitude)
        {
        	
            return new GeoPoint(888,493);
        }


    }
